package com.xuan.yubi.mq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class DLXDirectProducer {

    private static final String EXCHANGE_NAME = "another-topic-exchange";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        try (Connection connection = connectionFactory.newConnection();
             Channel channel = connection.createChannel()) {
            // 声明交换机名称和类型
            channel.exchangeDeclare(EXCHANGE_NAME, "topic");

            Scanner in = new Scanner(System.in);
            while (in.hasNext()) {
                String message = in.next();
                String routingKey = in.next();
                // 给每条消息设置 60 秒的过期时间
                AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
                                .expiration("60000").build();
                channel.basicPublish(EXCHANGE_NAME, routingKey, properties, message.getBytes(StandardCharsets.UTF_8));
                System.out.println(" [x] Sent '" + message + "'");
            }
        }
    }
}